package com.ooono.factorytest;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.ooono.factorytest.auto.AutoTestItemActivity;
import com.ooono.factorytest.utils.SPUtils;

public class AutoTestMainActivity extends AppCompatActivity {

    private Spinner testDateSpinner;
    private CheckBox testItemSpeakerCb;
    private CheckBox testItemVibratorCb;
    private CheckBox testItemLaserLedCb;
    private CheckBox testItemPilotCb;
    private CheckBox testItemFlashCb;
    private CheckBox testItemStrobeLedCb;
    private CheckBox textItemIrCb, textItemTricolorCb;
    private CheckBox testItemCameraCb;

    private boolean isTestSpeaker;
    private boolean isTestVibrator;
    private boolean isTestLaserLed;
    private boolean isTestPilot;
    private boolean isTestFlash;
    private boolean isTestIr, isTestTricolor;
    private boolean isTestStrobeLed;
    private boolean isTestCamera;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_test_main);
        testDateSpinner = findViewById(R.id.test_date);
        testDateSpinner.setSelection(7);

        testItemSpeakerCb = findViewById(R.id.cb_test_item_speaker);
        testItemVibratorCb = findViewById(R.id.cb_test_item_vibrator);
        testItemLaserLedCb = findViewById(R.id.cb_test_item_laser_led);
        testItemPilotCb = findViewById(R.id.cb_test_item_pilot);
        testItemFlashCb = findViewById(R.id.cb_test_item_flash);
        testItemStrobeLedCb = findViewById(R.id.cb_test_item_strobe_led);
        textItemIrCb = findViewById(R.id.cb_test_item_ir);
        textItemTricolorCb = findViewById(R.id.cb_test_item_tricolor);
        testItemCameraCb = findViewById(R.id.cb_test_item_camera);
        Button startTestBtn = findViewById(R.id.btn_start_test);
        startTestBtn.setOnClickListener(v -> {
            int selectedItemPosition = testDateSpinner.getSelectedItemPosition();
            isTestSpeaker = testItemSpeakerCb.isChecked();
            isTestVibrator = testItemVibratorCb.isChecked();
            isTestLaserLed = testItemLaserLedCb.isChecked();
            isTestPilot = testItemPilotCb.isChecked();
            isTestFlash = testItemFlashCb.isChecked();
            isTestStrobeLed = testItemStrobeLedCb.isChecked();
            isTestIr = textItemIrCb.isChecked();
            isTestTricolor = textItemTricolorCb.isChecked();
            isTestCamera = testItemCameraCb.isChecked();
            if (!isTestSpeaker && !isTestVibrator && !isTestLaserLed && !isTestPilot && !isTestFlash && !isTestStrobeLed && !isTestIr && !isTestTricolor && !isTestCamera) {
                Toast.makeText(this, "必须要选择一项进行测试", Toast.LENGTH_SHORT).show();
                return;
            }
            Intent intent = new Intent();
            intent.setClass(this, AutoTestItemActivity.class);
            intent.putExtra("testDate", selectedItemPosition + 1);
            intent.putExtra("speaker", isTestSpeaker);
            intent.putExtra("vibrator", isTestVibrator);
            intent.putExtra("laserLed", isTestLaserLed);
            intent.putExtra("pilot", isTestPilot);
            intent.putExtra("flash", isTestFlash);
            intent.putExtra("strobeLed", isTestStrobeLed);
            intent.putExtra("irLed", isTestIr);
            intent.putExtra("tricolorLed", isTestTricolor);
            intent.putExtra("camera", isTestCamera);
            startActivity(intent);
        });
        findViewById(R.id.btn_show_test_result).setOnClickListener(v -> {
            String[] testResult = new String[9];
            String testCameraTime = formatTime(SPUtils.get(this).getData("auto_camera", 0));
            testResult[8] = "摄像头测试：" + testCameraTime;
            String testSpeakerTime = formatTime(SPUtils.get(this).getData("auto_speaker", 0));
            testResult[0] = "喇叭测试：" + testSpeakerTime;
            String testVibratorTime = formatTime(SPUtils.get(this).getData("auto_vibrator", 0));
            testResult[1] = "振动测试：" + testVibratorTime;
            String testLaserLedTime = formatTime(SPUtils.get(this).getData("auto_laser_led", 0));
            testResult[2] = "激光灯测试：" + testLaserLedTime;
            String testPilotTime = formatTime(SPUtils.get(this).getData("auto_pilot", 0));
            testResult[3] = "指示灯测试：" + testPilotTime;
            String testFlashTime = formatTime(SPUtils.get(this).getData("auto_flash", 0));
            testResult[4] = "闪光灯测试：" + testFlashTime;
            String testStrobeLedTime = formatTime(SPUtils.get(this).getData("auto_strobe_led", 0));
            testResult[5] = "爆闪灯测试：" + testStrobeLedTime;
            String testIrLedTime = formatTime(SPUtils.get(this).getData("auto_ir_led", 0));
            testResult[6] = "红外灯测试：" + testIrLedTime;
            String testTricolorLedTime = formatTime(SPUtils.get(this).getData("auto_tricolor_led", 0));
            testResult[7] = "三色灯测试：" + testTricolorLedTime;
            new AlertDialog.Builder(this)
                    .setTitle("自动测试结果")
                    .setItems(testResult, null)
                    .setPositiveButton("确定", (d, which) -> d.dismiss()).create().show();
        });
    }

    private String formatTime(int second) {
        android.util.Log.d("tag_ooono", "aaaa:" + second);
        return second / (60 * 60) + " 时" + ((second % (60 * 60)) / 60) + " 分 " + (second % (60 * 60 * 60)) + " 秒";
    }
}
